<?php // @version $Id: default_item.php 9718 2007-12-20 22:35:36Z eddieajau $
defined('_JEXEC') or die('Restricted access');

// no direct access
$canEdit = ($this->user->authorize('com_content', 'edit', 'content', 'all') || $this->user->authorize('com_content', 'edit', 'content', 'own'));
?>

<?php 
// Strumenti per editing
// TODO Template non prevede l'editing da front
if ($this->user->authorize('com_content', 'edit', 'content', 'all') || $this->user->authorize('com_content', 'edit', 'content', 'own')) : ?>
<div class="contentpaneopen_edit<?php echo $this->params->get('pageclass_sfx'); ?>">
	<?php JHTML::_('icon.edit', $this->article, $this->params, $this->access); ?>
</div>
<?php 
endif;
// Fine strumenti per editing
?>


<?php
// Modifica e Creazione
if ((!empty ($this->article->modified) && $this->params->get('show_modify_date')) || ($this->params->get('show_author') && ($this->article->author != "")) || ($this->params->get('show_create_date'))) : 
?>
<p class="iteminfo">
	<?php 
	/*
	if (!empty ($this->article->modified) && $this->params->get('show_modify_date')) : ?>
	<span class="modifydate">
		<?php echo JText::_('Last Updated').' ('.JHTML::_('date', $this->article->modified, JText::_('DATE_FORMAT_LC2')).')'; ?>
	</span>
	<?php endif; 
	*/
	?>

	<?php
	/*
	if (($this->params->get('show_author')) && ($this->article->author != "")) : ?>
	<span class="createdby">
		<?php JText::printf('Written by', ($this->article->created_by_alias ? $this->article->created_by_alias : $this->article->author)); ?>
	</span>
	<?php endif; 
	*/
	?>

	<?php if ($this->params->get('show_create_date')) : ?>
	<span class="createdate">
		<?php echo JHTML::_('date', $this->article->created, JText::_('DATE_FORMAT_LC4')); ?>
	</span>
	<?php endif; ?>
</p>
<?php 
endif;
?>


<?php 
// Titolo Item
// TODO Template non prevede approfondimento per contenuto in home
$this->params->set('link_titles', false);
if ($this->params->get('show_title')) : 
?>
<h2 class="<?php echo $this->params->get('pageclass_sfx'); ?>">
	<?php 
	// Titolo come link?	
	if ($this->params->get('link_titles') && $this->article->readmore_link != '') : 
	?>
	
		<a href="<?php echo $this->article->readmore_link; ?>" class="contentpagetitle<?php echo $this->params->get('pageclass_sfx'); ?>">
			<?php echo $this->escape($this->article->title); ?>
		</a>

	<?php 
	else :
		echo $this->escape($this->article->title);
	endif; ?>
</h2>
<?php 
endif; 
?>

<?php if ($this->params->get('newspaper')) : ?>
<span class="createdate">
	testata: <strong><?php echo $this->params->get('newspaper'); ?></strong>
</span>
<?php endif; ?>


<?php 
// Output event AfterDesplayTitle
// TODO non so cosa accada, ma lascio l'output dei bot per gestire poi l'incident
if (!$this->params->get('show_intro')) :
	
	// Mostro output dei plugin dopo title
	echo $this->article->event->afterDisplayTitle;
endif; 
?>

<?php 
// Servizi mailto/print/pdf
// TODO Template non prevede i servizi mailto/print/pdf
/*if ($this->params->get('show_pdf_icon') || $this->params->get('show_print_icon') || $this->params->get('show_email_icon')) : 
?>
<p class="buttonheading">
	<img src="<?php echo $this->baseurl ?>/templates/beez/images/trans.gif" alt="<?php echo JText::_('attention open in a new window'); ?>" />
	<?php if ($this->params->get('show_pdf_icon')) :
		echo JHTML::_('icon.pdf',  $this->article, $this->params, $this->access);
	endif;
	if ($this->params->get('show_print_icon')) :
		echo JHTML::_('icon.print_popup',  $this->article, $this->params, $this->access);
	endif;
	if ($this->params->get('show_email_icon')) :
		echo JHTML::_('icon.email',  $this->article, $this->params, $this->access);
	endif; ?>
</p>
<?php
endif; 
*/
?>

<?php 
// Sezione e Categoria
// TODO Template non prevede l'evidenza della sezione/categoria del contenuto
/*
if (($this->params->get('show_section') && $this->article->sectionid) || ($this->params->get('show_category') && $this->article->catid)) : 
?>
<p class="pageinfo">
    <?php if ($this->params->get('show_section') && $this->article->sectionid && isset($this->article->section)) : ?>
	<span>
		<?php if ($this->params->get('link_section')) : ?>
			<?php echo '<a href="'.JRoute::_(ContentHelperRoute::getSectionRoute($this->article->sectionid)).'">'; ?>
		<?php endif; ?>
		<?php echo $this->article->section; ?>
		<?php if ($this->params->get('link_section')) : ?>
			<?php echo '</a>'; ?>
		<?php endif; ?>
		<?php if ($this->params->get('show_category')) : ?>
			<?php echo ' - '; ?>
		<?php endif; ?>
	</span>
	<?php endif; ?>
	<?php if ($this->params->get('show_category') && $this->article->catid) : ?>
	<span>
		<?php if ($this->params->get('link_category')) : ?>
			<?php echo '<a href="'.JRoute::_(ContentHelperRoute::getCategoryRoute($this->article->catslug, $this->article->sectionid)).'">'; ?>
		<?php endif; ?>
		<?php echo $this->article->category; ?>
		<?php if ($this->params->get('link_section')) : ?>
			<?php echo '</a>'; ?>
		<?php endif; ?>
	</span>
	<?php endif; ?>
</p>
<?php
endif; 
*/
?>

<?php 
// Output event BeforeDesplayContent
// TODO non so cosa accada, ma lascio l'output dei bot per gestire poi l'incident
echo $this->article->event->beforeDisplayContent; 
?>

<?php 
// Link al contenuto esploso
// TODO template non prevede tale funzionalità in home
/*
if ($this->params->get('show_url') && $this->article->urls) : 
?>
<span class="small">
	<a href="<?php echo $this->article->urls; ?>" target="_blank">
		<?php echo $this->article->urls; ?></a>
</span>
<?php 
endif;
*/ 
?>

<?php 
// TODO Table of Content non gestita e non so nemmeno cosa faccia!
/* 
if (isset ($this->article->toc)) :
	echo $this->article->toc;
endif;
*/ 
?>

<?php 
include("img_articole.php");

// Contenuto
echo JFilterOutput::ampReplace($this->article->text); 
?>

<?php 
// Link al contenuto esploso "Leggi tutto"
// TODO template non prevede tale funzionalità
/*
if ($this->params->get('show_readmore') && $this->article->readmore) : ?>
<p>
	<a href="<?php echo $this->article->readmore_link; ?>" class="readon<?php echo $this->params->get('pageclass_sfx'); ?>">
		<?php if ($this->article->readmore_register) :
			echo JText::_('Register to read more...');
		elseif ($readmore = $this->params->get('readmore')) :
			echo $readmore;
		else :
			echo JText::sprintf('Read more', $this->article->title);
		endif; ?></a>
</p>
<?php 
endif; 
*/
?>

<?php 
// Output event AfterDisplayContent
// TODO non so cosa accada, ma lascio l'output dei bot per gestire poi l'incident
echo $this->article->event->afterDisplayContent;
?>

<?php
$db =& JFactory::getDBO();
$user =& JFactory::getUser();
$aid = $user->get('aid', 0);

$nullDate	= $db->getNullDate();
$date =& JFactory::getDate();
$now = $date->toMySQL();
?>

<?php
// Determino informazione padre
if($this->params->get('parent')) {
	
	$where = 'a.state = 1'
		. ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
		. ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
		. ' AND a.id = '.$this->params->get('parent')
		;
	
	$query = 'SELECT a.*, ' .
		' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
		' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
		' FROM #__content AS a' .
		' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
		' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
		' WHERE '. $where .' AND s.id > 0' .
		($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
		' AND s.published = 1' .
		' AND cc.published = 1' .
		' ORDER BY created DESC';
	
	$db->setQuery($query);
	$parent = $db->loadObject();
	if($parent) {
	?>
	
		<div class="separatore"></div>
		<div class="attachmentsList">
			
			<?php
			// Titolo
			echo "<h3>In relazione a:</h3>";
			?>
			
			<ul>
				<?php
				$link = ContentHelperRoute::getArticleRoute($parent->slug, $parent->catslug, $parent->sectionid);
				$link = JRoute::_($link);
	
				$title = htmlspecialchars($parent->title );
				?>
				<li>
					<a href="<?php echo $link ?>">
						<?php echo $title ?>
					</a>
				</li>
			</ul>
		
		</div>
	
	<?php
	}
} 
?>

<?php
// Ricerco news associate
$word_ext = $db->Quote( '.*[.newline.]?' . $db->getEscaped('parent=' . $this->article->id, true ) . '([.newline.].*)?$', false );

$where = 'a.state = 1'
	. ' AND ( a.publish_up = '.$db->Quote($nullDate).' OR a.publish_up <= '.$db->Quote($now).' )'
	. ' AND ( a.publish_down = '.$db->Quote($nullDate).' OR a.publish_down >= '.$db->Quote($now).' )'
	. ' AND a.attribs REGEXP '.$word_ext
	;

$query = 'SELECT a.*, ' .
	' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
	' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug'.
	' FROM #__content AS a' .
	' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
	' INNER JOIN #__sections AS s ON s.id = a.sectionid' .
	' WHERE '. $where .' AND s.id > 0' .
	($access ? ' AND a.access <= ' .(int) $aid. ' AND cc.access <= ' .(int) $aid. ' AND s.access <= ' .(int) $aid : '').
	' AND s.published = 1' .
	' AND cc.published = 1' .
	' ORDER BY created DESC';

$db->setQuery($query, 0, 5);
$rows = $db->loadObjectList();
if($rows) {
?>

	<div class="separatore"></div>
	<div class="attachmentsList">
		
		<?php
		// Titolo
		echo "<h3>News associate:</h3>";
		?>
		
		<ul>
			<?php
			foreach ( $rows as $row ) {
				$link = ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid);
				$link = JRoute::_($link);

				$title = htmlspecialchars( $row->title );
				?>
				<li>
					<a href="<?php echo $link ?>">
						<?php echo $title ?>
					</a>
				</li>
				<?php       
			}	
			?>
		</ul>
	
	</div>

<?php
} 
?>
